package com.javasm.springmybatis.controller;

import com.javasm.springmybatis.mail.MailUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.concurrent.TimeUnit;

@RestController
public class EmailController {

    @Resource
    private MailUtil mailUtil;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("getEmailCode")
    public Boolean getEmailCode(String email){
        Integer code = (int)(Math.random() * 9000 + 1000);
        mailUtil.sendmail("您的验证码","您的验证码是"+code+"有效期五分钟",email);
        stringRedisTemplate.opsForValue().set("key_code_"+email, String.valueOf(code),5, TimeUnit.MINUTES);
        return true;
    }





    @PostMapping("login")
    public Boolean login(String email,String code){
        String trueCode = stringRedisTemplate.opsForValue().get("key_code_" + email);
        return code.equals(trueCode);
    }
}

